package com.hourse.app.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.internal.LinkedTreeMap;
import com.hourse.app.entity.CurrencyEntity;
import com.hourse.app.entity.TickerEntity;
import com.hourse.app.service.ConfigCurrencyService;
import com.hourse.app.service.DealService;
import com.hourse.app.service.PlatformAccountService;
import com.hourse.app.service.PlatformOyxService;
import com.hourse.app.utils.DateUtil;
import com.hourse.app.vo.rms.CurrencyVo;
import com.hourse.app.vo.rms.TradesRetVo;
import com.hourse.app.vo.web.PublicDealRetVo;
import com.hourse.okx.config.APIConfiguration;
import com.hourse.okx.enums.I18nEnum;
import com.hourse.okx.service.marketData.MarketDataAPIService;
import com.hourse.okx.service.marketData.impl.MarketDataAPIServiceImpl;
import com.hourse.platform.exception.AppException;
import com.hourse.platform.service.impl.PublicServiceImpl;
import com.hourse.platform.utils.AppTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @作者：lichenguang
 * @描述：【交易页面接口】
 * @创建：2022/2/15 21:08
 */
@Service
@Slf4j
public class DealServiceImpl extends PublicServiceImpl implements DealService {

  @Value("${okx.http.url}")
  private String okxHttpUrl;

  @Autowired
  private PlatformOyxService platformOyxService;

  @Autowired
  private ConfigCurrencyService configCurrencyService;

  //private APIConfiguration config = config();
  //
  //MarketDataAPIService marketDataAPIService = new MarketDataAPIServiceImpl(this.config);
  //
  //public APIConfiguration config() {
  //  APIConfiguration config = new APIConfiguration();
  //  config.setEndpoint("https://www.okx.com");
  //  // apiKey，api注册成功后页面上有
  //  config.setApiKey("8eb8f8d2-3e1d-4098-9d9b-7c1648e3fc8b");
  //  config.setSecretKey("C80F45392D6EAF93AFBF5DAAF77A13E8");
  //  config.setPassphrase("1okxlight");
  //
  //  config.setPrint(true);
  //  config.setI18n(I18nEnum.ENGLISH);
  //
  //  return config;
  //}

  @Override
  public HashMap<String, List<TradesRetVo>> getOrderBooks(String currency) throws AppException {
    log.info("欧易查询单个货币挂单信息（盘口）");

    return (HashMap<String, List<TradesRetVo>>) platformOyxService.getOrderBook(currency, this.getConfigNoAuth());
  }

  @Override
  public List<PublicDealRetVo> getTrans(String currency) throws AppException {
    log.info("欧易查询单个货币历史成交信息");

    return (List<PublicDealRetVo>) platformOyxService.getTrans(currency, this.getConfigNoAuth());
  }


  /**
   * @描述 ：【获取k线图】
   * @作者 ：lichenguang
   * @时间 ：2022/3/24 1:24
   */
  @Override
  public Object getK(String currency, String type) throws AppException {
    log.info("欧易查询单个货币K线图信息");
    try {
      switch (type) {
        case "1min":
          type = "1m";
          break;
        case "1hour":
          type = "1H";
          break;
        case "1day":
          type = "1D";
          break;
        case "1week":
          type = "1Wutc";
          break;
      }

      return platformOyxService.getK(currency, type, this.getConfigNoAuth());

    } catch (AppException e) {
      e.printStackTrace();
      throw new AppException("获取K线图失败");
    }

  }

  /**
   * @描述 ：【获取单个货币行情】
   * @作者 ：lichenguang
   * @时间 ：2022/3/24 23:31
   */
  @Override
  public Object ticker(String market) {

    return platformOyxService.getTicker(market, this.getConfigNoAuth());
  }

  @Override
  public Object tickerAll() {

    List<Object> retList = new ArrayList<>();

    List<Object> list = configCurrencyService.list();
    for (Object item : list) {
      CurrencyVo currencyVo = (CurrencyVo) item;
      TickerEntity ticker = (TickerEntity) platformOyxService.getTicker(currencyVo.getCode(), this.getConfigNoAuth());
      retList.add(ticker);
    }

    return retList;
  }

  /**
   * @描述 ：【获取欧易三方配置信息(不存在账户认证信息)】
   * @作者 ：lichenguang
   * @时间 ：2022/3/25 15:41
   */
  private APIConfiguration getConfigNoAuth() {

    APIConfiguration config = new APIConfiguration();
    config.setEndpoint(this.okxHttpUrl);
    config.setPrint(true);
    config.setI18n(I18nEnum.ENGLISH);

    return config;
  }

  public void toResultString(String flag, Object object) {
    StringBuilder su = new StringBuilder();
    su.append("\n").append("=====>").append(flag).append(":\n").append(JSON.toJSONString(object));
    log.info(su.toString());
  }

}
